package cn.tedu.knows.portal.controller;

import cn.tedu.knows.portal.exception.ServiceException;
import cn.tedu.knows.portal.service.IUserService;
import cn.tedu.knows.portal.vo.RegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class SystemController {

    @Autowired
    private IUserService userService;

    @PostMapping("/register")
    public String register(
            //RegisterVo参数前添加@Validated表示开启服务器端验证功能
            //
            @Validated RegisterVo registerVo,
            //这个参数必须紧随RegisterVo之后
            //
            BindingResult result){

        log.debug("接收用户信息:{}",registerVo);
        if(result.hasErrors()){
            String msg = result.getFieldError().getDefaultMessage();
            return msg;
        }
        try {
            userService.registerStudent(registerVo);
            return "ok";
        }catch (ServiceException e){
            log.error("注册失败",e);
            return e.getMessage();
        }
    }


}
